﻿SET QUOTED_IDENTIFIER ON
SET ARITHABORT ON
SET NUMERIC_ROUNDABORT OFF
SET CONCAT_NULL_YIELDS_NULL ON
SET ANSI_NULLS ON
SET ANSI_PADDING ON
SET ANSI_WARNINGS ON


-- Augmentation de la longeueur de la colonne password a 512

DECLARE @Users TABLE (UserId UNIQUEIDENTIFIER, Password NVARCHAR(MAX))
INSERT INTO @USERS(UserID, Password)
SELECT UserId, Password FROM medianamik_User
	
ALTER TABLE dbo.medianamik_User
	DROP COLUMN Password
	
ALTER TABLE dbo.medianamik_User ADD
	Password nvarchar(512) NOT NULL CONSTRAINT DF_medianamik_User_Password_NotNull DEFAULT ''
	
UPDATE dbo.medianamik_User SET Password = u.Password
FROM  dbo.medianamik_User
JOIN @Users u on u.UserId = dbo.medianamik_User.UserId


ALTER TABLE dbo.medianamik_User
	DROP CONSTRAINT DF_medianamik_User_Password_NotNull
	
	
-- Ajout de la colonne Salt
IF NOT EXISTS(
Select * FROM Information_Schema.COLUMNS
where TABLE_SCHEMA = 'dbo'
AND TABLE_NAME = 'medianamik_User'
AND Column_Name = 'Salt'
)
BEGIN
	ALTER TABLE dbo.medianamik_User ADD
	Salt nvarchar(128) NOT NULL CONSTRAINT DF_medianamik_User_SALT_NotNull DEFAULT ''
	
	ALTER TABLE dbo.medianamik_User
	DROP CONSTRAINT DF_medianamik_User_SALT_NotNull
	
END

-- Ajout de la colonne PasswordFormat
IF NOT EXISTS(
Select * FROM Information_Schema.COLUMNS
where TABLE_SCHEMA = 'dbo'
AND TABLE_NAME = 'medianamik_User'
AND Column_Name = 'PasswordFormat'
)
BEGIN
	ALTER TABLE dbo.medianamik_User ADD
	PasswordFormat INT NOT NULL CONSTRAINT DF_medianamik_User_PasswordFormat_NotNull DEFAULT 0
	
	ALTER TABLE dbo.medianamik_User
	DROP CONSTRAINT DF_medianamik_User_PasswordFormat_NotNull
	
END